//@version=6
indicator(title="ADX Filtered Average True Range", shorttitle="FilterATR", overlay=false, timeframe="", timeframe_gaps=true)

var GATR = "ADX Filtered ATR"
atrlen = input.int(title="ATR - Length", defval=13, options = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946], group = GATR, inline = "1")
smoothing = input.string(title="Smoothing", defval="RMA", options=["SWMA", "RMA", "SMA", "EMA", "VWMA", "WMA"], group = GATR, inline = "1")

adxlen = input.int(34, title="ADX - Length", options = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946], group = GATR, inline = "2")
lensig = input.int(3, title="Smoothing", options = [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946], group = GATR, inline = "2")

[diplus, diminus, adx] = ta.dmi(adxlen, lensig)

sadx = ta.swma(adx)
// plot(sadx, color=color.teal, title="ADX")

ADXUp = sadx > sadx[1] and adx > sadx and (diplus > diplus[1] or diplus > diminus) and diplus > 15
ADXDn = sadx > sadx[1] and adx > sadx and (diminus > diminus[1] or diminus > diplus) and diminus > 15

ma_function(source, atrlen) =>
    switch smoothing
        "SWMA"=> ta.swma(source)
        "RMA" => ta.rma(source, atrlen)
        "SMA" => ta.sma(source, atrlen)
        "EMA" => ta.ema(source, atrlen)
        "VWMA" => ta.vwma(source, atrlen)
        => ta.wma(source, atrlen)

atr = ma_function(ta.tr(true), atrlen)
atrs = ta.swma(atr)

ATRExpanding = (atrs > atrs[1] and atr > atrs)

plot(atr, title = "ATR", color=color.new(#B71C1C, 0))
plot(atrs, title = "ATRS", color = ATRExpanding ? color.green : color.white, linewidth = 3)

bgcolor(ATRExpanding and ADXUp and close > open ? color.new(color.green, 50) : ATRExpanding and ADXDn and close < open ? color.new(color.red, 50) : na)